//
//  HTPaymentCountDownButton.m
//  HTPaymentCountDownButton
//

//  获取效验码按钮

#import "HTPaymentCountDownButton.h"

@implementation HTPaymentCountDownButton
#pragma mark - super
- (instancetype)init{
    if (self = [super init]) {
        [self setTitleColor:HTPaymentCommonCountDwonColor forState:UIControlStateNormal];
        [self setTitleColor:HTPaymentCommonCountDwonColor forState:UIControlStateDisabled];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setTitleColor:HTPaymentCommonCountDwonColor forState:UIControlStateNormal];
        [self setTitleColor:HTPaymentCommonCountDwonColor forState:UIControlStateDisabled];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setTitleColor:HTPaymentCommonCountDwonColor forState:UIControlStateNormal];
        [self setTitleColor:HTPaymentCommonCountDwonColor forState:UIControlStateDisabled];
    }
    return self;
}

#pragma -mark touche action
- (void)HTPaymentCountDownButtonHandler:(TouchedHTPaymentCountDownButtonHandler)touchedHTPaymentCountDownButtonHandler{

    _touchedHTPaymentCountDownButtonHandler = [touchedHTPaymentCountDownButtonHandler copy];
    [self addTarget:self action:@selector(touched:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)touched:(HTPaymentCountDownButton*)sender{
    if (_touchedHTPaymentCountDownButtonHandler) {
        _touchedHTPaymentCountDownButtonHandler(sender,sender.tag);
    }
}

#pragma -mark count down method
-(void)startCountDownWithSecond:(NSUInteger)totalSecond
{
    _totalSecond = totalSecond;
    _second = totalSecond;
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerStart:) userInfo:nil repeats:YES];
    _startDate = [NSDate date];
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
}
-(void)timerStart:(NSTimer *)theTimer {
     double deltaTime = [[NSDate date] timeIntervalSinceDate:_startDate];
    
     _second = _totalSecond - (NSInteger)(deltaTime+0.5) ;

    
    if (_second< 0.0)
    {
        [self stopCountDown];
    }
    else
    {
        if (_countDownChanging){
            [self setTitle:_countDownChanging(self,_second) forState:UIControlStateNormal];
            [self setTitle:_countDownChanging(self,_second) forState:UIControlStateDisabled];
            [self setAttributedTitle:[self getNewAttributeString:_countDownChanging(self,_second) titleColor:[UIColor lightGrayColor]] forState:UIControlStateNormal];
            [self setAttributedTitle:[self getNewAttributeString:_countDownChanging(self,_second) titleColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled];
        }else{
            NSString *title = [NSString stringWithFormat:@"%zd秒",_second];
            [self setTitle:title forState:UIControlStateNormal];
            [self setTitle:title forState:UIControlStateDisabled];
            [self setAttributedTitle:[self getNewAttributeString:title titleColor:[UIColor lightGrayColor]] forState:UIControlStateNormal];
            [self setAttributedTitle:[self getNewAttributeString:title titleColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled];
        }
    }
}

- (void)stopCountDown{
    if (_timer) {
        if ([_timer respondsToSelector:@selector(isValid)])
        {
            if ([_timer isValid])
            {
                [_timer invalidate];
                _second = _totalSecond;
                if (_countDownFinished)
                {
                    [self setTitle:_countDownFinished(self,_totalSecond)forState:UIControlStateNormal];
                    [self setTitle:_countDownFinished(self,_totalSecond)forState:UIControlStateDisabled];

                    [self setAttributedTitle:[self getNewAttributeString:_countDownFinished(self,_totalSecond) ] forState:UIControlStateNormal];
                    [self setAttributedTitle:[self getNewAttributeString:_countDownFinished(self,_totalSecond) ] forState:UIControlStateDisabled];
                }
                else
                {
                    [self setTitle:@"重新发送" forState:UIControlStateNormal];
                    [self setTitle:@"重新发送" forState:UIControlStateDisabled];

                    [self setAttributedTitle:[self getNewAttributeString:@"重新发送"] forState:UIControlStateNormal];
                    [self setAttributedTitle:[self getNewAttributeString:@"重新发送"] forState:UIControlStateDisabled ];
                }
            }
        }
    }
}


- (NSMutableAttributedString *)getNewAttributeString:(NSString *)Str titleColor:(UIColor *)color{
    
     NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:Str attributes:@{NSForegroundColorAttributeName:color,NSFontAttributeName:HTPaymentRealFont(15),NSUnderlineStyleAttributeName:@(1),NSUnderlineColorAttributeName:color}];
    return att;
}

- (NSMutableAttributedString *)getNewAttributeString:(NSString *)Str{
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:Str attributes:@{NSForegroundColorAttributeName:HTPaymentCommonCountDwonColor,NSFontAttributeName:HTPaymentRealFont(15),NSUnderlineStyleAttributeName:@(1),NSUnderlineColorAttributeName:HTPaymentCommonCountDwonColor}];
    return att;
}


- (BOOL)checkThePureDigital:(NSString *)str{
    return [self checkStr:str withRegExp:@"^[0-9]*$"];
}

- (BOOL)checkStr:(NSString *)str withRegExp:(NSString *)RegExp{
    if (str.length ==0 || RegExp.length == 0) {
        return NO;
    }
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", RegExp];
    BOOL result = [pred evaluateWithObject:str];
    return result;
}

#pragma -mark block
-(void)countDownChanging:(CountDownChanging)countDownChanging{
    _countDownChanging = [countDownChanging copy];
}
-(void)countDownFinished:(CountDownFinished)countDownFinished{
    _countDownFinished = [countDownFinished copy];
}
@end
